﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace GMAC.ReportingSystem.Commom
{
    //------------------------------------------------------------
    /// <summary>
    /// Ricardo Recaredo (28/03/2011)
    /// 
    /// Item Template Control to store the number of calls.
    /// </summary>
    //------------------------------------------------------------

    public class CallReasonAgentTemplate : ITemplate
    {
        public string DataField { get; set; }
        public string ControlID { get; set; }

        //--------------------------------------------------

        public CallReasonAgentTemplate(string dataField, string controlID)
        {
            DataField = dataField;
            ControlID = controlID;
        }

        //--------------------------------------------------

        public void InstantiateIn(Control container)
        {
            //Control Textbox

            var textBox             = new TextBox();
            textBox.ID              = ControlID;
            textBox.EnableViewState = true;
            textBox.MaxLength       = 3;
            textBox.Width           = new Unit(30);
            textBox.DataBinding    += label_DataBinding;
            textBox.ValidationGroup = "DynamicData";

            textBox.Style.Add("margin-left", "5px");
            textBox.Style.Add("margin-right", "5px");

            //Eventos

            textBox.Attributes.Add("onblur", "CheckNullValue(this)");

            //Validaciones

            #region Código Comentado pero util para futuro

            //var rfv = new RequiredFieldValidator();

            //rfv.ControlToValidate = textBox.ClientID;                                   //-------------------------------------------------------------------------//
            //rfv.ID                = "rvf_" + ControlID + "_" + DateTime.Now.ToString(); // Este código lo comenté debido a que agregué un método cuando el control //
            //rfv.Text              = "Error";                                            // pierde focus, que setea en 0 en valor del texbox si este está vacio.    //
            //rfv.SetFocusOnError   = true;                                               //-------------------------------------------------------------------------//

            #endregion

            AjaxControlToolkit.FilteredTextBoxExtender filteredTextBoxExtender = new AjaxControlToolkit.FilteredTextBoxExtender();

            filteredTextBoxExtender.FilterType      = AjaxControlToolkit.FilterTypes.Numbers;
            filteredTextBoxExtender.TargetControlID = textBox.ClientID;
            filteredTextBoxExtender.ID              = "ftbe_" + ControlID + "_" + Utils.RandomString(5);

            //Se agregan los controles nuevos al Container Template

            container.Controls.Add(textBox);
            container.Controls.Add(filteredTextBoxExtender);
        }

        //--------------------------------------------------

        void label_DataBinding(object sender, EventArgs e)
        {
            var textbox     = (TextBox)sender;
            GridViewRow row = (GridViewRow)textbox.NamingContainer;
            textbox.Text    = DataBinder.Eval(row.DataItem, DataField).ToString();
        }
    }
}
